<?php
/**
 *幼儿园首页
 *
 **/
namespace Home\Controller;

use Vendor\Page;

class UserController extends ComController
{
    public function index()
    {
        $this->display('user_center');
    }
    //给老师留言
    public function massage(){
        if(IS_POST){
            $data['user_id'] = 1;
            $data['uid'] = $_POST['uid'];
            $data['content'] = $_POST['content'];
            if(empty($data['content'])){
                echo 2;exit;
            }
            $data['add_time'] = date('Y-m-d H:i:s');
            $data['status'] = 2;
            $res = M('massage')->add($data);
            if($res){
                echo 1;exit;
            }else{
                echo 0;exit;
            }
        }
        $this->display();
    }
    //宝宝列表
    public function body_list(){
        $body =  M('body')
                ->field("sc_body.body_name,sc_body.img,c.name,m.name as m_name,m.uid")
                ->join("sc_baoming as b on sc_body.id=b.body_id","LEFT")
                ->join("sc_class as c on c.id = b.class_id","LEFT")
                ->join("sc_member as m on m.uid = b.sc_id","LEFT")
                ->where(array('sc_body.user_id'=>1))
                ->select();
        $this->assign('body',$body);
        $this->display('baby-list');
    }
    //互动消息
    public function hdmassage(){
        //给老师的留言
        $data = M('answer')
                ->field('sc_answer.content as a_content,s.content as s_content,m.head')
                ->join("sc_massage as s on s.id = sc_answer.massage_id","LEFT")
                ->join("sc_member as m on m.uid = s.uid","LEFT")
                ->where(array('sc_answer.user_id = 1'))
                ->order('s.add_time desc')
                ->select();
        //给学校的建议
        $arr = M('feed_back')
                ->field("sc_feed_back.content,m.head")
                ->join("sc_member as m on m.uid=sc_feed_back.sc_id","LEFT")
                ->where(array('user_id'=>1))->order("sc_feed_back.add_time desc")->select();
        $this->assign('arr',$arr);
        $this->assign('data',$data);
        $this->display('message-center');
    }
    //在线报名
    public function getApply(){
        $class_type = M('class_type')->where(array('sc_id'=>$_GET['uid']))->select();
        $this->assign('class_type',$class_type);
        $this->assign('sc_id',$_GET['uid']);
        $this->display();
    }
    //获取班级信息
    public function getClass(){
        $class = M('class')->where(array('class_type'=>$_POST['id'],'is_del'=>1))->select();
        $str = "<option>请选择班级</option>";
        foreach ($class as $k=>$v){
            $str.="<option value='".$v['id']."'>".$v['name']."</option>";
        }
        echo $str;exit;
    }
    //获取班级报名价格
    public function getClassPrice(){
        $id = I('post.id');
        $class = M('class')->field('id,price')->where(array('id'=>$_POST['id']))->find();
        echo json_encode($class);exit;
    }
    //报名
    public function putApply(){
        $baoming['user_id'] = 1;
        $baoming['sc_id'] = I('post.sc_id');
        $baoming['class_id'] = I('post.class_id');
        $baoming['price'] = I('post.price');
        $baoming['phone'] = I('post.phone');
        $baoming['id_card'] = I('post.id_card');
        $body['body_name'] = I('post.name');
        $body['id_card'] = I('post.id_card');
        $body['user_id'] = 1;
        $status = M('baoming')->where(array('id_card'=>I('post.id_card')))->find();
        if($status){
            echo 4;exit;//已经报过名
        }
        if($body['body_name']==''||$body['id_card']==''||$body['user_id']==''){
            echo 2;exit;//请完善信息
        }
        $res = M('body')->add($body);
        if($res){
           $baoming['body_id'] = M('body')->getLastInsID();
           $baoming['add_time'] = date('Y-m-d H:i:s');
           if($baoming['user_id']==''||$baoming['sc_id']==''|| $baoming['class_id']=='' ||$baoming['price']==''){
               echo 3;//请完善资料
           }
           $res_b = M('baoming')->add($baoming);
           if($res_b){
               echo 1;exit;//成功
           }else{
               $flag = 0;
               $res_d = M('body')->where(array('id'=>$baoming['body_id']))->delete();
               if($res_d === false){
                   $flag = 0;
               }else{
                   $flag = 1;
               }
               if($flag == 1){
                   echo 0;//失败
               }
           }
           
        }else{
           echo 0;exit;//失败 
        }
        
    }
}